from django.db import models

from goods.models import *
from users.models import *


# 收货地址表
class Addr(models.Model):
    receiver = models.CharField('收件人', max_length=20)
    receiver_mobile = models.CharField('收件人手机号', max_length=11)
    receiver_addr = models.CharField('收件人地址', max_length=100)
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='所属用户',)

    def __str__(self):
        return '收件人:%s' % self.receiver

    class Meta:
        db_table = 'address'


# 订单信息表
class OrderInfo(models.Model):
    PAY_STATUS = (
        (1, '待支付'),
        (2, '待发货'),
        (3, '待收货'),
        (4, '待评价'),
        (5, '已完成')
    )
    PAY_METHOD = (
        (1, '支付宝'),
        (2, '微信支付'),
        (3, '银联支付'),
        (4, '货到付款'),
    )
    order_id = models.CharField('订单编号', max_length=64, primary_key=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='orders', verbose_name='下单用户',)
    addr = models.ForeignKey(Addr, on_delete=models.CASCADE, verbose_name='收货地址',)
    total_count = models.IntegerField('商品总数', default=1)
    total_amount = models.DecimalField('总价格', max_digits=10, decimal_places=2, )
    freight = models.DecimalField('运费', max_digits=10, decimal_places=2, )
    pay_method = models.SmallIntegerField('支付方式', choices=PAY_METHOD, default=1)
    status = models.SmallIntegerField('支付状态', choices=PAY_STATUS, default=1)
    created_time = models.DateTimeField('下单时间', auto_now_add=True)

    def __str__(self):
        return self.order_id

    class Meta:
        db_table = 'order_info'


# 订单商品表
class OrderGood(models.Model):

    sku = models.ForeignKey(Good, on_delete=models.CASCADE, verbose_name='订单商品',)
    count = models.IntegerField('购买的数量', default=1)
    price = models.DecimalField('价格', max_digits=10, decimal_places=2, )
    order = models.ForeignKey(OrderInfo, on_delete=models.CASCADE, verbose_name='所属订单',related_name='orderGoods')
    comment = models.TextField('评论信息', default='')
    score = models.SmallIntegerField('评分', default=5)
    is_anonymous = models.BooleanField('是否匿名评价', default=False)
    is_commented = models.BooleanField('是否评价', default=False)

    def __str__(self):
        return '%s:%s' % (self.order_id, self.sku.name)

    class Meta:
        db_table = 'order_goods'

# 支付信息
# trade_id   varchar(100) 唯一 可为空 '支付编号'
# order 订单fk
class PaymentInfo(models.Model):
    trade_id = models.CharField('支付编号', blank=True, unique=True, null=True, max_length=100, )
    order = models.ForeignKey(OrderInfo, on_delete=models.CASCADE, verbose_name='所属订单')

    def __str__(self):
        return '%s:%s' % (self.order_id, self.trade_id)

    class Meta:
        db_table = 'payment_info'

